package com.situ.employee2024.controller;

import com.situ.employee2024.model.Employee;
import com.situ.employee2024.service.EmployeeService;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.util.List;
import java.util.Map;

/**
 * 功能：
 *
 * @author 千堆雪
 * @version 1.0.0
 * @since 2024/1/9
 * <p>
 * created by 千堆雪 on 2024/1/9, last modified by 千堆雪 on 2024/1/9
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    //创建业务类的实例，多态
    private final EmployeeService service;

    /*@Autowired
    public void setService(EmployeeService service) {
        this.service = service;
    }*/

    public EmployeeController(EmployeeService service) {
        this.service = service;
    }

    /**
     * 展示一个员工列表
     */
    @GetMapping("/list")
    public String list() {
        //调用业务类的方法
        //List<Employee> employeeList = service.findAll();

        //将员工列表放到请求域中
        //map.put("list", employeeList);

        return "employee/list";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    public String add() {
        return "employee/add";
    }


    /**
     * 跳转到修改页面
     */
    @GetMapping("/edit")
    public String edit(Integer id, Map<String, Object> map) {
        Employee employee = service.findById(id);
        if (employee == null) {
            map.put("error", "指定编号的员工不存在");
        } else {
            map.put("emp", employee);

        }
        return "employee/edit";
    }


}
